package priv.conceit.sc.hoxton.file.handle.compress;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
import org.springframework.stereotype.Component;
import priv.conceit.sc.hoxton.common.entity.file.dto.FilePathDTO;
import priv.conceit.sc.hoxton.common.util.IoUtil;
import priv.conceit.sc.hoxton.file.util.PdfUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * sc-hoxton
 *
 * @author Conceit
 * 2020/6/12 增加方法
 * @since 1.0.0, 2020/6/12
 */
@Component
public class SevenZCompressHandle extends AbsCompressHandle{

	/**
	 * unCompress 解压7z文件
	 *
	 * @param filePathDTO 封装的文件信息
	 * @param inputStream 文件输入流
	 * @param convertFile 转换过之后的文件
	 * @return void
	 * @author conceit
	 * @date 2020/6/15 17:12
	 */
	@Override
	public boolean unCompress(FilePathDTO filePathDTO, InputStream inputStream, File convertFile)  {
		byte[] bytes= IoUtil.toByteArray(inputStream);
		SeekableInMemoryByteChannel seekable=new SeekableInMemoryByteChannel(bytes);
		try {
			SevenZFile sevenZFile=new SevenZFile(seekable);
			List<String> items = new ArrayList<>();
			Iterator<SevenZArchiveEntry> iterator=sevenZFile.getEntries().iterator();
			while (iterator.hasNext()){
				SevenZArchiveEntry sevenZArchiveEntry=iterator.next();
				items.add(sevenZArchiveEntry.getName());
			}
			PdfUtil pdfUtil=new PdfUtil();
			pdfUtil.generatePdf(convertFile,filePathDTO.getOriginalName(),items);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;


	}
}
